<template>
  <div class="app-container">
    <el-form
      label-position="top"
      :model="formClient"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="108px"
      class="noteAdd"
      style="border-bottom: 2px solid #8c939d;">
      <div style="margin-top: 20px;display: flex;justify-content: center;">
          <el-form-item label-width="120px" label="Company Name" prop="name">
            <el-input
              v-model="formClient.name"
              placeholder=""
              disabled
            />
          </el-form-item>
          <el-form-item label="Co Size" prop="coSize">
            <el-input
              v-model="formClient.coSize"
              placeholder=""
              disabled
            />
          </el-form-item>
      </div>
      <div style="display: flex;justify-content: center;">
          <el-form-item label-width="120px" label="Industry" prop="industry">
            <el-input
              v-model="formClient.industry"
              placeholder=""
              disabled
            />
          </el-form-item>
          <el-form-item label="Address" prop="address">
            <el-input
              v-model="formClient.address"
              placeholder=""
              disabled
            />
          </el-form-item>
      </div>
      <div style="display: flex;justify-content: center;">
          <el-form-item label-width="120px" label="Contact" prop="contact1">
            <el-input
              v-model="formClient.contact1"
              placeholder=""
              disabled
            />
          </el-form-item>
          <el-form-item label="Phone" prop="tel11">
            <el-input
              v-model="formClient.tel11"
              placeholder=""
              disabled
            />
          </el-form-item>
      </div>
      <!--<div style="display: flex;justify-content: center;">-->
          <!--<el-form-item label-width="120px" label="Website" prop="website">-->
            <!--<el-input-->
              <!--v-model="formClient.website"-->
              <!--placeholder=""-->
              <!--disabled-->
            <!--/>-->
          <!--</el-form-item>-->
          <!--<el-form-item label-width="120px" label="Website" prop="website" style="visibility: hidden;">-->
            <!--<el-input-->
              <!--v-model="formClient.website"-->
              <!--placeholder=""-->
              <!--disabled-->
            <!--/>-->
          <!--</el-form-item>-->
      <!--</div>-->
    </el-form>

    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    style="margin-top: 20px;">
      <el-form-item label="Title" prop="createTime">
        <el-input
          style="width: 200px;"
          v-model="queryParams.title"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Salary" prop="salary">
        <el-input
          style="width: 100px;margin-right: 10px;"
          v-model="queryParams.minSalary"
          placeholder="From"
          clearable
          @keyup.enter.native="handleQuery"
        />
        <span>-</span>
        <el-input
          style="width: 100px;margin-left: 10px;"
          v-model="queryParams.maxSalary"
          placeholder="To"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">Search</el-button>
        <el-button v-if="false" icon="el-icon-refresh" size="mini" @click="resetQuery">Reset</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >Add</el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
        <!--&gt;修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="danger"-->
          <!--plain-->
          <!--icon="el-icon-delete"-->
          <!--size="mini"-->
          <!--:disabled="multiple"-->
          <!--@click="handleDelete"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="warning"-->
          <!--plain-->
          <!--icon="el-icon-download"-->
          <!--size="mini"-->
          <!--@click="handleExport"-->
          <!--v-hasPermi="['system:position:export']"-->
        <!--&gt;导出</el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      border
      v-loading="loading"
      :data="positionList"
      @selection-change="handleSelectionChange">
      <el-table-column
        width="120px"
        label="Action"
        align="center"
        class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >Edit</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAssign(scope.row)"
          >Assign</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['system:position:del']"
            @click="handleDelete(scope.row)"
            >Del</el-button>
        </template>
      </el-table-column>
      <el-table-column label="Title" align="left" prop="title" />
      <el-table-column label="Salary" align="left" prop="salary" />
      <el-table-column label="Salary Cap" align="left" prop="salaryupper" />
      <el-table-column label="Report To" align="left" prop="reportTo" />
      <el-table-column label="Education" align="left" prop="education" />
      <el-table-column label="Assignment" align="left" prop="assignName" />
      <el-table-column label="Create Time" align="left" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="Assignment"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <!--<div style="display: flex;justify-content: center;margin-top: -30px;">-->
        <!--<span style="color: #000;font-size: 18px;">Please Select A Candidate</span>-->
      <!--</div>-->
      <el-form
        :model="queryParamsUser"
        ref="queryFormUser"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        style="margin-top: 20px;">
        <el-form-item label="Name" prop="userName">
          <el-input
            v-model="queryParamsUser.userName"
            placeholder=""
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item label="Name" prop="" style="display: none;">
          <el-input
            placeholder=""
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleUserQuery">Search</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="candidateList"
        style="margin-top: 0px;">
        <el-table-column
          width="60px"
          label="action"
          align="left"
          class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handleAddUserCand(scope.row)"
            >Assign</el-button>
          </template>
        </el-table-column>
        <el-table-column label="name" align="center" key="userName" prop="userName" />
        <el-table-column label="nick" align="center" key="nickName" prop="nickName" />
        <el-table-column label="phone" align="center" key="phonenumber" prop="phonenumber" />
      </el-table>

      <pagination
        v-show="totalUser>0"
        :total="totalUser"
        :page.sync="queryParamsUser.pageNum"
        :limit.sync="queryParamsUser.pageSize"
        @pagination="getCandList"
      />
    </el-dialog>

  </div>
</template>

<script>
import { listPosition, getPosition, delPosition, addPosition, updatePosition } from "@/api/system/position";
import { listClient, getClient, delClient, addClient, updateClient } from "@/api/system/client";
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus} from "@/api/system/user";

export default {
  name: "Position",
  data() {
    return {
      candidateList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      clientId: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalUser: 0,
      // 【请填写功能名称】表格数据
      positionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParamsUser: {
        pageNum: 1,
        pageSize: 10,
        userName: null
      },
      queryParamsClient: {
        pageNum: 1,
        pageSize: 10,
        id: null
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        clientId: null,
        title: null,
        salary: null,
        minSalary: null,
        maxSalary: null,
        salaryupper: null,
        reportTo: null,
        education: null,
        jobDuties: null,
        specialReq: null
      },
      itemForm: {},
      // 表单参数
      form: {},
      formClient: {
        id: null,
        name: null,
        industry: null,
        busiNature: null,
        address: null,
        coSize: null,
        contact1: null,
        pos1: null,
        tel11: null,
        tel12: null,
        email1: null,
        contact2: null,
        pos2: null,
        tel21: null,
        tel22: null,
        email2: null,
        lastAssignee: null,
        website: null,
        remark: null,
        filePath: null
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: "$comment null", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // this.getCandList();
    console.log("created");
    if (this.$route.query.id == null || this.$route.query.id == '' || this.$route.query.id  == undefined) {
    } else {
      this.clientId = this.$route.query.id;
      this.queryParamsClient.id = this.$route.query.id;
      this.queryParams.clientId = this.$route.query.id;
      this.form.clientId = this.$route.query.id;
      this.getClientList();
      this.getList();
    }
  },
  methods: {
    handleAssign(row) {
      this.open = true;
      this.itemForm = row;
    },
    handleAddUserCand(row){
      //分配任务
      this.$modal.loading("loading...");
      this.itemForm.assignedUserId = row.userId;
      updatePosition(this.itemForm).then(response => {
        this.$modal.msgSuccess("success");
      }).catch(err => {
        this.$modal.msgError("Fail,Please Again");
      }).finally(f => {
        this.open = false;
        this.$modal.closeLoading();
      });
    },
    handleUserQuery() {
      this.queryParamsUser.pageNum = 1;
      this.getCandList();
    },
    getCandList() {
      // this.loading = true;
      listUser(this.queryParamsUser).then(response => {
        this.candidateList = response.rows;
        this.totalUser = response.total;
        // this.loading = false;
      });
    },
    getClientList() {
      listClient(this.queryParamsClient).then(response => {
        if (response.rows != null) {
          this.formClient = response.rows[0];
        }
      }).finally (f => {
      });
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listPosition(this.queryParams).then(response => {
        this.positionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        createUserId: null,
        title: null,
        clientId: null,
        salary: null,
        salaryupper: null,
        reportTo: null,
        education: null,
        jobDuties: null,
        specialReq: null,
        bftal: null,
        bftmedical: null,
        bftdental: null,
        bftbonus: null,
        bftdblpay: null,
        bftcomss: null,
        bftother: null,
        remarks: null,
        closed: null,
        serial: null,
        posCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      const obj = { path: "/system/position/positionAdd", name: "positionAdd" };
      this.$tab.closePage(obj);
      setTimeout(()=> {
        this.$router.push({path:'/system/position/positionAdd',query: {clientId: this.clientId}})
      }, 300);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const obj = { path: "/system/position/positionAdd", name: "positionAdd" };
      this.$tab.closePage(obj);
      setTimeout(()=> {
        this.$router.push({path:'/system/position/positionAdd',query: {id: row.id, clientId: row.clientId}})
      }, 300);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('Delete？').then(function() {
        return delPosition(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("success");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/position/export', {
        ...this.queryParams
      }, `position_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
  .noteAdd .el-form-item__label {
    line-height: 0px;
  }
</style>
